package edu.gyu.myshop.controller;


import edu.gyu.myshop.model.*;
import edu.gyu.myshop.model.TrackingNumbers;
import edu.gyu.myshop.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.math.BigDecimal;


/**
 * <p>
 * 快递单号表 前端控制器
 * </p>
 *
 * @author ls
 * @since 2024-06-30
 */
@Controller
public class TrackingNumbersController {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    MyuserService myuserService;
    @Autowired
    UserAddressService userAddressService;
    @Autowired
    OrderDetailService orderDetailService;
    @Autowired
    MyorderService myorderService;
    @Autowired
    TrackingNumbersService trackingNumberService;

    @GetMapping("/admin/shiporder/{id}")
    public String shipOrder(@PathVariable("id") int id, Model model) {
        OrderDetail orderDetail = orderDetailService.getById(id);
        orderDetail.setProductName(goodsService.getById(orderDetail.getGoodsid()).getName());
        userAddressService.list().forEach(userAddress -> {
                    if (userAddress.getUserId() == orderDetail.getUserid()) {
                        orderDetail.setAddress(userAddress.getAddress());
                        orderDetail.setPhone(userAddress.getPhone());
                        orderDetail.setUsername(userAddress.getName());
                    }
                }
        );
        orderDetail.setStatus(String.valueOf(myorderService.getById(orderDetail.getOrderid()).getStatus()));
        orderDetail.setPicture(goodsService.getById(orderDetail.getGoodsid()).getPicture());
        model.addAttribute("orderDetail", orderDetail);
        return "pages/shiporder";
    }

    @RequestMapping("/admin/shiporderpost")
    public String shipOrderPost(OrderDetail orderDetail, RedirectAttributes redirectAttributes) {
        System.out.println("==================");
        System.out.println(orderDetail);
        System.out.println(orderDetail.getPrice());
        TrackingNumbers trackingNumbers = new TrackingNumbers(Long.valueOf(orderDetail.getId()), orderDetail.getShippingCompany(), orderDetail.getTrackingNumber());
        trackingNumberService.save(trackingNumbers);
        System.out.println(orderDetail);
        Myorder one = myorderService.lambdaQuery().eq(Myorder::getId, orderDetail.getOrderid()).one();
        one.setStatus(2);
        myorderService.updateById(one);
        redirectAttributes.addAttribute("msg2", "发货成功");
        return "redirect:/admin/orderList";
    }


}
